import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.Timer;
	
public class TimerMan extends JPanel
{
	private int timeField;
	private Dimension johnny;
	private  Timer timer;
	private JLabel timerLabel;
	private TimeClass tc;
	
	public TimerMan(Dimension size, int time)
	{
		johnny = size;
		//setLayout(new GridLayout(2,2,5,5));
		timerLabel = new JLabel("WAITING", SwingConstants.CENTER);
		timerLabel.setFont(new Font("Courier New", Font.BOLD, this.johnny.height - 7)); 
	    timerLabel.setForeground(Color.RED);
		add(timerLabel);
		timeField = time; //sets timer to 10 seconds.
		timerLabel.setText("" + timeField);
		
		tc = new TimeClass(timeField);
		timer = new Timer(1000, tc);
		timer.start();
		
	}
	
	public Dimension getPreferredSize()
	{
		return this.johnny;
	}
	
	public int getTime()
	{
		return timeField;
	}
	
	public Timer getTimer()
	{
		return timer;
	}
	
	public void restartTimer()
	{
	    TimeClass lc = new TimeClass(timeField + 1); //restarts timer
		timer = new Timer(1000, lc);
		timer.start();
	}
	
	public void stopTimer()
	{
		tc.stopTimer();
	}
	
	public void updateTime()
	{
		tc.updateTime();
	}
	
	

	public class TimeClass implements ActionListener
	{
		private int counter;
		private boolean isRunning;
		
		public TimeClass(int counter)
		{
			this.isRunning = true;
			this.counter = counter;
		}
		
		public void updateTime()
		{
			timeField = counter;
		}
		
		public void stopTimer()
		{
			this.isRunning = false;
		}

		public void actionPerformed(ActionEvent tc)
		{
			if(this.isRunning)
			{
				counter--;
				if (counter >= 1)
				{
					timerLabel.setText("" + counter);
				}
				else
				{
					timer.stop();
					timerLabel.setText("0");
					Toolkit.getDefaultToolkit().beep();
				}
			}
		}
		
	}
	

		
	}
	
	
		
		
	
	
